<?php
$loader->requireOnce('includes/RadProcessor/ARadProcessor.class.php');
$loader->requireOnce('controllers/C_Endoscopy.class.php');
class EndoscopyProcessor extends ARadProcessor {
    private $logger;
    public function __construct($radType){
        parent::__construct($radType, new C_Endoscopy());
        $this->logger =  LoggerManager::getLogger('modules.dept_rad.includes.RadProcessor.EndoscopyProcessor');
    }

    public function loadAssignmentInputDetails($ticketId, $triggeredElement){
       $this->controller->assign('ticketId', $ticketId);
        $this->controller->assign('radType', $this->enumRadType);
        // Date order
        $orders =   L10nHelper::getYMDorder();
        $this->controller->assign('datePart_0', $orders[0]);
        $this->controller->assign('datePart_1', $orders[1]);
        $this->controller->assign('datePart_2', $orders[2]);
        // Current datetime
        $curDate        =   L10nHelper::renderDateTime(date('Y-m-d'));
        $createDate     =   L10nHelper::getDateParts($curDate);
        $createdTime    =   date('H:i');
        $this->controller->assign('createdDate', $createDate);
        $this->controller->assign('createdTime', $createdTime);
        // Doctor list
        $departmentId   =   $this->controller->_me->get_department_id();
        $doctorList =   ORDataObject::factory('Employee')->getEmployeeOfDepartment($departmentId, EmployeeType::DOCTOR_ID, true);
        $this->controller->assign('doctorList', $doctorList);
        // Group list
        $groupList =   ORDataObject::factory('RadGroup')->getRadGroupsByEnumRadType($this->enumRadType);
        $this->controller->assign('groupList', $groupList);

        // Triggered element
        $this->controller->assign('triggeredElement', $triggeredElement);
        return $this->controller->view->render('assignment_create.html');
    }

    public function saveAssignmentTicket($data){
       $created['day']     =   Utils::sanitizeText($data['created_dd']);
        $created['month']   =   Utils::sanitizeText($data['created_mm']);
        $created['year']    =   Utils::sanitizeText($data['created_yyyy']);
        $createdTime        =   L10nHelper::getISODate($created) . " " . Utils::sanitizeText($data['createdTime']);

        // Patient type, patient Id from the Ticket
        $objTicket          =   Celini::newORDO('Ticket', $data['ticketId']);
        $enumPatientType    =   $objTicket->getEnumPatientType();
        $patientId          =   $objTicket->getPatientId();

        // Status for the assignment
        $enumStatus =   0;
        if ($enumPatientType == Utils::getEnumKey(EnumerationConstant::PATIENT_TYPE, EnumerationConstant::PATIENT_TYPE_INPATIENT)) {
            $enumStatus =   Utils::getEnumKey(EnumerationConstant::RAD_TICKET_STATUS, EnumerationConstant::RAD_TICKET_STATUS_WAITING);
        }
        elseif ($enumPatientType == Utils::getEnumKey(EnumerationConstant::PATIENT_TYPE, EnumerationConstant::PATIENT_TYPE_OUTPATIENT)) {
            $enumStatus    =   Utils::getEnumKey(EnumerationConstant::RAD_TICKET_STATUS, EnumerationConstant::RAD_TICKET_STATUS_ASSIGNED);
        }

        // Save assignment ticket
        $diagnosis      =   Utils::sanitizeText($data['diagnosis'], false);
        $objRadTicket   =   ORDataObject::factory('RadTicket');
        $objRadTicket->setData($data['ticketId'], $patientId, $enumPatientType, $data['radType'],
            $data['doctorId'], $data['departmentId'], $createdTime, $diagnosis, $enumStatus);
        $objRadTicket->persist();
        $radTicketId    =   $objRadTicket->get_id();

        // Save assignment items
        $i  =   0;
        $totalOrgPrice  =   0;
        $totalInsPrice  =   0;
        $toalPatPrice   =   0;
        while(isset($data['services'][$i])) {
            $serviceId  =   $data['services'][$i];
            $prices     =   ORDataObject::factory('Service')->getPrices($data['ticketId'], $serviceId, EnumerationConstant::SER_INS_ITEM_TYPE_PTTT);
            $objRadItem =   ORDataObject::factory('RadTicketItem');
            $objRadItem->setData($radTicketId, $serviceId, $prices['price'], $prices['insurance_price'], $prices['patient_price'], 0);
            $objRadItem->persist();
            $totalOrgPrice  +=  $prices['price'];
            $totalInsPrice  +=  $prices['insurance_price'];
            $toalPatPrice   +=  $prices['patient_price'];
            $i++;
        }
        $ret['radTicketId']     =   $radTicketId;
        $ret['originalPrice']   =   $totalOrgPrice;
        $ret['insurancePrice']  =   $totalInsPrice;
        $ret['patientPrice']    =   $toalPatPrice;
        return $ret;
    }

    public function loadAssignmentEdit($radTicketId, $triggeredElement) {
        $this->logger->info("Start funtion loadAssignmentEdit {radTicketId=$radTicketId, triggeredElement=$triggeredElement}");
        $objRadTicket   =   Celini::newORDO('RadTicket', $radTicketId);
        // Get items of the radiology ticket
        $serviceList    =   ORDataObject::factory('RadTicketItem')->getServicesByTicket($radTicketId);

        // Get total prices and perform some utils
        $total  =   array('original_price'  =>  0, 'insurance_price'  =>  0, 'patient_price'  =>  0);
        foreach ($serviceList as &$service) {
            $total['original_price']    +=  $service['original_price'];
            $total['insurance_price']   +=  $service['insurance_price'];
            $total['patient_price']     +=  $service['patient_price'];
            $service['original_price']  =   L10nHelper::renderNumber($service['original_price']);
            $service['insurance_price'] =   L10nHelper::renderNumber($service['insurance_price']);
            $service['patient_price']   =   L10nHelper::renderNumber($service['patient_price']);
        }
        $total['original_price']    =  L10nHelper::renderNumber($total['original_price']);
        $total['insurance_price']   =  L10nHelper::renderNumber($total['insurance_price']);
        $total['patient_price']     =  L10nHelper::renderNumber($total['patient_price']);
        // Assign smarty variables
        $this->controller->assign_by_ref('radTicket', $objRadTicket);
        $this->controller->assign('radItemList', $serviceList);
        $this->controller->assign('total', $total);
        $this->controller->assign('index', 1);

        // Common info
        // Date order
        $orders =   L10nHelper::getYMDorder();
        $this->controller->assign('datePart_0', $orders[0]);
        $this->controller->assign('datePart_1', $orders[1]);
        $this->controller->assign('datePart_2', $orders[2]);
        // Request time
        $requestTime    =   $objRadTicket->getLocalizedDate('request_time');
        $createdDate     =   L10nHelper::getDateParts($requestTime);
        $createdTime    =   $objRadTicket->getLocalizedDate('request_time', 'short', 'time');
        $this->controller->assign('createdDate', $createdDate);
        $this->controller->assign('createdTime', $createdTime);
        // Doctor list
        $departmentId   =   $this->controller->_me->get_department_id();
        $doctorList =   ORDataObject::factory('Employee')->getEmployeeOfDepartment($departmentId, EmployeeType::DOCTOR_ID, true);
        $this->controller->assign('doctorList', $doctorList);
        // Group list
        $groupList =   ORDataObject::factory('RadGroup')->getRadGroupsByEnumRadType($this->enumRadType);
        $this->controller->assign('groupList', $groupList);
        // Triggered element
        $this->controller->assign('triggeredElement', $triggeredElement);
        $this->controller->assign('radTicketId', $radTicketId);

        $this->logger->info("Finish funtion loadAssignmentEdit {radTicketId=$radTicketId, triggeredElement=$triggeredElement}");
        return $this->controller->view->render('assigment_edit.html');
    }

    public function loadResultInput($radTicketId) {
        $this->logger->info("Start funtion loadResultInput {radTicketId=$radTicketId}");
        $objRadTicket  =   Celini::newORDO('RadTicket', $radTicketId);
        $doctorName    =   Celini::newORDO('Person', $objRadTicket->getTreatmentDoctorId())->getName();
        $diagnosis     =   $objRadTicket->getDiagnosis();
        $itemList      =   ORDataObject::factory('RadTicketItem')->getServicesByTicket($radTicketId, true, true);
        $this->controller->assign('treatmentDoctorName', $doctorName);
        $this->controller->assign('preDiagnosis', nl2br($diagnosis));
        $this->controller->assign('radTicketItemList', $itemList);

        // Technician and doctor list
        $objEmployee    =   ORDataObject::factory("Employee");
        $techDocList    =   $objEmployee->getEmployeeOfDepartment($this->controller->_me->get_department_id(),
            array(EmployeeType::DOCTOR_ID,EmployeeType::TECHNICAN_ID), true);
        $docList        =   $objEmployee->getEmployeeOfDepartment($this->controller->_me->get_department_id(),
            EmployeeType::DOCTOR_ID, true);
        $this->controller->assign('techList', $techDocList);
        $this->controller->assign('doctorList', $docList);

        // Additional services
        $additionalServiceList  =   $this->getAddiotionalServices();
        if (count($additionalServiceList) > 0) {
            $this->controller->assign('additionalServiceList', $additionalServiceList);
        }

        // Date order
        $orders =   L10nHelper::getYMDorder();
        $this->controller->assign('datePart_0', $orders[0]);
        $this->controller->assign('datePart_1', $orders[1]);
        $this->controller->assign('datePart_2', $orders[2]);

        $result =   array(
            'itemList'   =>   '',
            'meta'       =>   '',
            'conclusion' =>   ''
        );
        $result['itemList']     =   $this->controller->view->render('result_input_item_list.html');
        $result['meta']         =   $this->controller->view->render('result_input_meta.html');
        $result['conclusion']   =   $this->controller->view->render('result_input_conclusion.html');
        $this->logger->info("Finsih funtion loadResultInput {radTicketId=$radTicketId}");

        return $result;
    }

    public function loadResultView($radTicketId) {
        $this->logger->info("Start funtion loadResultView {radTicketId=$radTicketId}");
        $objRadTicket  =   Celini::newORDO('RadTicket', $radTicketId);
        $doctorName    =   Celini::newORDO('Person', $objRadTicket->getTreatmentDoctorId())->getName();
        $diagnosis     =   $objRadTicket->getDiagnosis();
        $itemList      =   ORDataObject::factory('RadTicketItem')->getServicesByTicket($radTicketId, true, true);
        $this->controller->assign('treatmentDoctorName', $doctorName);
        $this->controller->assign('preDiagnosis', nl2br($diagnosis));
        $this->controller->assign('radTicketItemList', $itemList);


        // Technician and doctor list
        $objEmployee    =   ORDataObject::factory("Employee");
        $techDocList    =   $objEmployee->getEmployeeOfDepartment($this->controller->_me->get_department_id(),
            array(EmployeeType::DOCTOR_ID,EmployeeType::TECHNICAN_ID), true);
        $docList        =   $objEmployee->getEmployeeOfDepartment($this->controller->_me->get_department_id(),
            EmployeeType::DOCTOR_ID, true);
        $this->controller->assign('techList', $techDocList);
        $this->controller->assign('doctorList', $docList);

        // Additional services
        $additionalServiceList  =   $this->getAddiotionalServices();
        if (count($additionalServiceList) > 0) {
            $this->controller->assign('additionalServiceList', $additionalServiceList);
        }
        $result =   array(
            'itemList'   =>   '',
            'meta'       =>   '',
            'conclusion' =>   ''
        );
        $result['itemList']     =   $this->controller->view->render('result_view_item_list.html');
        $result['meta']         =   $this->controller->view->render('result_view_meta.html');
        $result['conclusion']   =   $this->controller->view->render('result_view_conclusion.html');
        $this->logger->info("Finsih funtion loadResultView {radTicketId=$radTicketId}");

        return $result;
    }
}
?>